################################## 
############ Preamble ############ 
################################## 

# set language to English
Sys.setenv(LANG = "en")

# clean up
rm(list = ls())

# Set working directory: please set your own
#setwd("~/Dropbox/JOP third submission/JOP replication/")

# Load necessary packages
library(tidyverse)
library(haven)
library(psych)


# Import dataset
data <- read.dta13("01_data/elections_ciutadella.dta")

pp_trends_plot <- data %>%
  mutate(Election = ifelse(ep == 1, "European", "Other")) %>%
  ggplot(aes(x = period, y = pp_voteshare, color = Election)) +
  geom_point(position = position_dodge(width = 0.6), alpha = 0.1) +
  scale_color_manual(values = c("blue", "red")) +
  stat_summary(fun = mean, geom = "point", position = position_dodge(width = 0.05)) +
  stat_summary(fun = mean, geom = "line", position = position_dodge(width = 0.05)) +
  scale_x_continuous(breaks = c(1, 2, 3), 
                     labels = c("2 elections before (2009-11)", "1 election before (2014-15)", "2019")) +
  scale_y_continuous(limits = c(0, 70)) +
  geom_vline(xintercept = 2.5, linetype = 2, color = "black") +
  theme_bw() +
  labs(y = " ", x = " ") 
pp_trends_plot

ggsave("04_plots/figured4.png", plot = pp_trends_plot, width = 12, height = 10, units = "cm")